/*
 * @Author: liyan liyan@norinaviation.com
 * @Date: 2022-12-06 16:26:28
 * @LastEditors: liyan liyan@norinaviation.com
 * @LastEditTime: 2022-12-06 16:33:44
 * @FilePath: \notam\src\utils\arrayToTree.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { reactive } from "vue"

export function arrayToTree(data) {

    const list = reactive([]);
    const obj = reactive({})

    data.forEach(item => {
        if (!item.children) {
            item.children = []
        }
        obj[item.id] = item
    })

    data.forEach(item => {
        const parent = obj[item.parentId]
        if (parent) {
            parent.children.push(item)
        } else {
            list.push(item)
        }
    })

    return list
}